package com.lc.user.express.my;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.adapter.IntegraAdapter;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.GetExchangecoupon;
import com.lc.user.express.httpserver.GetMyIntegral;
import com.lc.user.express.model.MyIntegralModel;
import com.lc.user.express.utils.GetToast;
import com.lc.user.express.view.MyListView;
import com.lc.user.express.view.dialog.AffirmDialog;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/14.
 */
public class MyIntegralActivity extends BaseSecondActivity {

    private MyListView list_integral;
    private IntegraAdapter integraAdapter;
    private List<MyIntegralModel> list = new ArrayList<>();
    private TextView number;//可用积分
    TextView text_integral;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral);
        setTitle("我的积分");
        initView();
        getData();
    }

    private void initView() {
        number = (TextView) findViewById(R.id.number);
        list_integral = (MyListView) findViewById(R.id.list_integral);
        integraAdapter = new IntegraAdapter(cnt, list);
        text_integral = (TextView) findViewById(R.id.text_integral);

        list_integral.setAdapter(integraAdapter);
        list_integral.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                new AffirmDialog(cnt, "确定兑换该优惠券吗？", "取消", "确定", R.style.MyDialog) {

                    @Override
                    protected void onYesClick() {
                        new GetExchangecoupon(AppContext.myPreferences.getUid(), list.get(position).getId(), new AsyCallBack<Object>() {
                            @Override
                            public void onEnd(String toast, int type) throws Exception {
                                super.onEnd(toast, type);
                                if(!TextUtils.isEmpty(toast)) GetToast.useString(cnt,toast);

                            }
                            @Override
                            public void onSuccess(String toast, int type, Object o) throws Exception {
                                super.onSuccess(toast, type, o);
                                if("1".equals(o)){
                                    if (MyFragment.onReFresh != null) {
                                        MyFragment.onReFresh.onRefresh();
                                    }
                                    getData();
                                }


                            }
                        }).execute(cnt);
                    }
                }.show();
            }
        });
    }

    private void getData() {
        new GetMyIntegral(AppContext.myPreferences.getUid(), new AsyCallBack<GetMyIntegral.Info>() {
            @Override
            public void onSuccess(String toast, int type, GetMyIntegral.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                number.setText(info.number + "分");
                list.clear();
                list.addAll(info.datalist);
                integraAdapter.updateListView(list);
                text_integral.setText("积分是用户在168速运消费获得的奖励\n消费"+info.money + "元得" + info.integral + "积分（不包括优惠券消费）\n" +
                        "  可在168速运积分平台兑换优惠券");
            }
        }).execute(cnt);//调接口

    }
}
